﻿#include "TouchToolWidget.h"
#include "ui_TouchToolWidget.h"
#include "components/BlinkBorderWindow/BlinkBorderWindow.h"
#include "utils/Color/ColorGen.h"

#include <QDebug>

TouchToolWidget::TouchToolWidget(QWidget *parent)
    : QDockWidget(parent)
    , ui(new Ui::TouchToolWidget)
    , m_handler(new TouchHandler(this))
    , m_ptw(new PointTrackWindow(this))
    , m_injector(new TouchInjector)
{
    ui->setupUi(this);

    connect(m_handler, &TouchHandler::touched, this, &TouchToolWidget::onTouched);

    ui->btnStart->setOnIcon(QIcon(":/imgs/stop.png"));
    m_ptw->hide();
    on_btnRefreshScreens_clicked();
}

TouchToolWidget::~TouchToolWidget()
{
    delete m_injector;
    m_injector = nullptr;

    delete ui;
}

void TouchToolWidget::setUiEnabled(bool en)
{
    ui->cbScreen->setEnabled(en);
    ui->btnRefreshScreens->setEnabled(en);
    ui->cbFuncSelect->setEnabled(en);
}

void TouchToolWidget::onTouched(const QList<TouchPoint> &points)
{
    if (!ui->btnStart->isOn()) return;

    const MonitorData& monitor = m_monitors[ui->cbScreen->currentIndex()];
    QList<TouchPoint> logicPoints = points;

    QStringList msg;
    for (TouchPoint& point: logicPoints) {
        msg << point.toString();
        QPoint logic = monitor.toLogical(point.pos());
        point.setPos(logic);
    }
    if (ui->checkBox->isChecked()) ui->tbLog->append(msg.join("  "));

    uint8_t fid = ui->cbFuncSelect->currentIndex();
    if (fid == 0) {  // 系统触摸，需要物理坐标
        m_injector->inject(points, TouchInjector::WorkMode::Touch);
    } else if (fid == 1) {  // 系统鼠标，需要物理坐标
        m_injector->inject(points, TouchInjector::WorkMode::Mouse);
    } else if (fid == 2) {  // 触摸画线，需要逻辑坐标
        m_ptw->setPoints(logicPoints);
    }
}

void TouchToolWidget::on_btnRefreshScreens_clicked()
{
    ui->cbScreen->clear();
    m_monitors = MonitorInfo::getAllMonitors();
    for (const MonitorData& monitor : m_monitors) {
        QString name = QString("%1: <%2> %3").arg(monitor.systemName()).arg(monitor.manufacturer()).arg(monitor.model());
        ui->cbScreen->addItem(name);
    }
}


void TouchToolWidget::on_cbScreen_activated(int index)
{
    const MonitorData& monitor = m_monitors[index];
    // 边框闪烁
    const QRect& rect = monitor.geometry();
    m_ptw->setGeometry(rect);
    m_injector->setRect(rect);
    const QColor& color = ColorGen::getColor(index);
    BlinkBorderWindow* bbw = new BlinkBorderWindow(rect, this, color);  // 闪烁完会自动释放内存
    bbw->show();
}

void TouchToolWidget::on_btnStart_clicked()
{
    bool isOn = ui->btnStart->isOn();
    ui->btnStart->setIconStatus(!isOn);
    setUiEnabled(isOn);
    if (isOn) {
        m_ptw->hide();
    } else {
        const MonitorData& monitor = m_monitors[ui->cbScreen->currentIndex()];
        const QRect& rect = monitor.geometry();
        m_ptw->setGeometry(rect);
        m_ptw->show();
        m_ptw->setOpacity(ui->hsOpacity->value());
        m_injector->setRect(rect);
        m_injector->setOrientation(monitor.orientation());
    }
}


void TouchToolWidget::on_btnClearLog_clicked()
{
    ui->tbLog->clear();
}


void TouchToolWidget::on_hsOpacity_valueChanged(int value)
{
    m_ptw->setOpacity(value);
    ui->lOpacity->setText(QString::number(value));
}


void TouchToolWidget::on_btnClearPoint_clicked()
{
    m_ptw->clear();
}

